package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;

public class OthersStudentDao1 implements BaseStudentDao {
    private static ArrayList<Student> stu =new ArrayList<>();
    /*private static TreeSet<Student> stu = new TreeSet<>(
            new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    int result = o1.getAge().compareTo(o2.getAge());
                    result = result == 0 ? o1.getId().compareTo(o2.getId()) : result;
                    return result;
                }
            }
    );
*/
    static {
        Student stu1 = new Student("heima001", "李丹丹", "23", "1999");
        Student stu2 = new Student("heima002", "李文杰", "21", "1999");
        stu.add(stu1);
        stu.add(stu2);
    }

    public boolean addStudenet(Student student) {

        return stu.add(student);
    }

    //查看学生
    public Student[] findAllStudent() {
        return stu.toArray(new Student[stu.size()]);
    }

    //删除学生
    public void deleteStudent(String id) {
        int index = getIndex(id);
        //
        stu.remove(index);
    }

    //获取指定学号在数组中的索引
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < stu.size(); i++) {
            if (stu.get(i) != null && id.equals(stu.get(i).getId())) {
                index = i;
            }
        }
        return index;
    }

    //修改学生
    public void updateStudent(Student student, String id) {
        int index = getIndex(id);
        stu.set(index, student);
    }
}
